package blueBridgeCup;

import java.util.LinkedList;
import java.util.Queue;

public class demo6 {
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
          this.left = left;
          this.right = right;
      }
  }


    public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null)return true;
else if (p==null||q==null) return false;
        Queue<TreeNode>queueq1=new LinkedList<>();
        Queue<TreeNode>queueq2=new LinkedList<>();

        queueq1.offer(p);
        queueq2.offer(q);

        while (!queueq1.isEmpty()&&!queueq2.isEmpty()){
            TreeNode node1=queueq1.poll();
            TreeNode node2=queueq2.poll();
            if(node1.val!=node2.val)return false;
            TreeNode left1=node1.left,right1=node1.right,left2=node2.left,right2=node2.right;
            if(left1==null^left2==null)return false;
            if(right1==null^right2==null)return false;
            if(left1!=null)queueq1.offer(left1);
            if(right1!=null)queueq1.offer(right1);
            if(left2!=null)queueq2.offer(left2);
            if(right2!=null)queueq2.offer(right2);
        }

return queueq1.isEmpty()&&queueq2.isEmpty();
    }

}
